package betAndWin.gui;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.ImageObserver;

import javax.swing.JComponent;

import betAndWin.application.GameControl;
import betAndWin.application.thread.HorseThread;


public class HorseComponent extends JComponent {

	private HorseThread horse;

	private Image horseImage;
	private int x;
	private int y;
	private int position;
	private ImageObserver imgObs;

	public HorseComponent(Image pHorseImage) {
		this.horseImage = pHorseImage;
		this.setOpaque(false);
		this.setDoubleBuffered(true);
	}

	public void setHorseThread(HorseThread pThread) {
		this.horse = pThread;
	}
	
	public void resetAttributes() {
		this.x = 0;
		this.y = 0;
		this.position = 0;
		this.imgObs = null;
		this.horse = null;
	}

	public void setImageObserver(ImageObserver pImgObs) {
		this.imgObs = pImgObs;
	}

	public int getActualX() {
		return this.x;
	}

	public int getActualY() {
		return this.y;
	}

	public Image getImage() {
		return this.horseImage;
	}

	protected void paintComponent(Graphics g) {
		int gameWidth = getWidth() - GameControl.END_POSITION;
		double scale = (double)gameWidth / GameControl.CORSIA_LENGTH;

		this.position = this.horse.getPosition();
		this.x = (int) (this.position * scale);

		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(this.horseImage, this.x, this.y, this.imgObs);
	}
}
